package utils;

import java.util.GregorianCalendar;
import java.util.Observable;

import Controller.Controller;

public class Horloge extends Observable implements Runnable {

	/**
	 * The actual time for our IHM.
	 */
	private final Duration duration;
	/**
	 * Wait in ms for a better view in IHM.
	 */
	private final int coeffWait;

	public GregorianCalendar getactualTime() {
		return duration.getBegin();
	}

	public Horloge(int coeffWait, Duration d) {
		this.coeffWait = coeffWait;
		this.duration = d;
	}

	@Override
	public void run() {
		long begin = duration.getBegin().getTimeInMillis();
		long end = duration.getEnd().getTimeInMillis();
		while (begin < end) {
			synchronized (Controller.getController()) {

				duration.getBegin().add(GregorianCalendar.MILLISECOND, 1);
				setChanged();
				notifyObservers();
				try {
					Thread.sleep(coeffWait);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				begin = duration.getBegin().getTimeInMillis();
			}

		}
		System.exit(0);
	}
}
